{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Panel Demo\n",
    "\n",
    "<img align=\"right\" src=\"https://anitagraser.github.io/movingpandas/assets/img/movingpandas.png\">\n",
    "\n",
    "This notebook demonstrates the use of **Holoviz [Panel](https://panel.holoviz.org/index.html)**. \n",
    "\n",
    "*Panel is an open-source Python library that lets you create custom interactive web apps and dashboards by connecting user-defined widgets to plots, images, tables, or text.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import urllib\n",
    "import os\n",
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "from geopandas import GeoDataFrame, read_file\n",
    "from shapely.geometry import Point, LineString, Polygon\n",
    "from pyproj import CRS\n",
    "from datetime import datetime, timedelta\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"..\")\n",
    "import movingpandas as mpd\n",
    "\n",
    "import warnings\n",
    "warnings.simplefilter(\"ignore\")\n",
    "\n",
    "import panel as pn\n",
    "import panel.widgets as pnw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = read_file('../data/geolife_small.gpkg')\n",
    "df['t'] = pd.to_datetime(df['t'])\n",
    "df = df.set_index('t').tz_localize(None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "traj_collection = mpd.TrajectoryCollection(df, 'trajectory_id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_traj = traj_collection.trajectories[1].to_crs(CRS(4088))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_traj.is_latlon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_plot(tolerance=0.001, generalizer='douglas-peucker'):\n",
    "    if generalizer=='douglas-peucker':\n",
    "        generalized = mpd.DouglasPeuckerGeneralizer(my_traj).generalize(tolerance=tolerance)\n",
    "    else:\n",
    "        generalized = mpd.MinDistanceGeneralizer(my_traj).generalize(tolerance=tolerance)\n",
    "    generalized.add_speed(overwrite=True)\n",
    "    return ( generalized.hvplot(title='Trajectory {}'.format(generalized.id), c='speed', cmap='Viridis', colorbar=True, clim=(0,20), line_width=10, width=500, height=500) + \n",
    "             generalized.df['speed'].hvplot.hist(title='Speed histogram', width=300, height=500) \n",
    "           )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kw = dict(tolerance=(0, 1000), generalizer=['douglas-peucker', 'min-distance'])\n",
    "pn.interact(my_plot, **kw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_generalized(traj_id=1, tolerance=10, generalizer='douglas-peucker'):\n",
    "    my_traj = traj_collection.get_trajectory(traj_id).to_crs(CRS(4088))\n",
    "    if generalizer=='douglas-peucker':\n",
    "        generalized = mpd.DouglasPeuckerGeneralizer(my_traj).generalize(tolerance)\n",
    "    else:\n",
    "        generalized = mpd.MinDistanceGeneralizer(my_traj).generalize(tolerance)\n",
    "    generalized.add_speed(overwrite=True)\n",
    "    return ( \n",
    "        generalized.hvplot(title='Trajectory {} (tolerance={})'.format(my_traj.id, tolerance), c='speed', cmap='Viridis', colorbar=True, clim=(0,20), line_width=10, width=500, height=500) + \n",
    "        generalized.df['speed'].hvplot.hist(title='Speed histogram', width=300, height=500) \n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kw = dict(traj_id=(1, len(traj_collection)), tolerance=(10, 100, 10), generalizer=['douglas-peucker', 'min-distance'])\n",
    "pn.interact(plot_generalized, **kw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
